<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Dayrui Website Management System
 *
 * @since		version 2.7.0
 * @author		Dayrui <dayrui@gmail.com>
 * @license     http://www.dayrui.com/license
 * @copyright   Copyright (c) 2011 - 9999, Dayrui.Com, Inc.
 */
	
class Notice extends M_Controller {

    /**
     * 构造函数
     */
    public function __construct() {
        parent::__construct();
        $this->template->assign('notice', $this->_notice());
    }

    /**
     * 提醒跳转
     */
    public function go() {

        $data = $this->db->select('type')->where('uid', (int)$this->uid)->where('isnew', 1)->get('member_notice_'.$this->member['tableid'])->row_array();
        if (!$data) {
            redirect(dr_member_url('notice/index'), 'refresh');
        } elseif ($data['type'] == 1) {
            redirect(dr_member_url('notice/index'), 'refresh');
        } elseif ($data['type'] == 2) {
            redirect(dr_member_url('notice/member'), 'refresh');
        } elseif ($data['type'] == 3) {
            redirect(dr_member_url('notice/module'), 'refresh');
        } elseif ($data['type'] == 4) {
            redirect(dr_member_url('notice/app'), 'refresh');
        } else {
            redirect(dr_member_url('notice/index'), 'refresh');
        }

    }

    /**
     * 系统提醒
     */
    public function index() {
		$this->m_notice(1);
    }
	
	/**
     * 会员提醒
     */
    public function member() {
		$this->m_notice(2);
    }
	
	/**
     * 模块提醒
     */
    public function module() {
		$this->m_notice(3);
    }
	
	/**
     * 应用提醒
     */
    public function app() {
		$this->m_notice(4);
    }
	
	/**
     * 提醒查看
     */
    private function m_notice($type) {

        $name = array(
            1 => fc_lang('系统提醒'),
            2 => fc_lang('互动提醒'),
            3 => fc_lang('模块提醒'),
            4 => fc_lang('应用提醒'),
        );
		$this->db->where('uid', (int)$this->uid)->where('type', (int)$type)->order_by('inputtime DESC');
			 
		if (IS_POST) {
			$this->db->where_in('id', $this->input->post('ids'))->delete('member_notice_'.$this->member['tableid']);
			exit(dr_json(1, fc_lang('操作成功，正在刷新...')));
		}

        $page = max((int)$this->input->get('page'), 1);
		if ($this->input->get('action') == 'more') {
		    // ajax更多数据
			$data = $this->db->limit($this->pagesize, $this->pagesize * ($page - 1))->get('member_notice_'.$this->member['tableid'])->result_array();
			if (!$data) {
                exit('null');
            }
			$this->template->assign('list', $data);
			$this->template->display('notice_data.html');
		} else {
			$this->template->assign(array(
                'page' => max(2, $page + 1),
				'list' => $this->db->limit($page ? $page * $this->pagesize : $this->pagesize)->get('member_notice_'.$this->member['tableid'])->result_array(),
				'moreurl' => dr_member_url($this->router->class.'/'.$this->router->method).'&action=more',
                'meta_name' => $name[$type],
			));
			// 更新新提醒
			$this->db->where('uid', (int)$this->uid)->where('type', (int)$type)->update('member_notice_'.$this->member['tableid'],
                array('isnew' => 0)
            );
            // 删除新提醒
			$this->db->where('uid', (int)$this->uid)->delete('member_new_notice');
			$this->template->display('notice_index.html');
		}
		
	}
}